//
//  ShakeNaviController.swift
//  FunSeeker
//
//  Created by 王泽宇 on 2017/4/4.
//  Copyright © 2017年 edu.Tongji.sse. All rights reserved.
//

import UIKit

class ShakeNaviController: UIViewController, AMapSearchDelegate, AMapLocationManagerDelegate, MAMapViewDelegate {
    var search = AMapSearchAPI()
    var locationManager = AMapLocationManager()
    var itemInfo: AMapPOI?
    var selfAnnotation : MAPointAnnotation?
    var destAnnotation : MAPointAnnotation?
    var mapView : MAMapView?
    
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var addressLabel: UILabel!
    @IBOutlet weak var distanceLabel: UILabel!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var mapBackView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        search.route
        // Do any additional setup after loading the view.
        self.navigationItem.title = "路线"
        let req = AMapTransitRouteSearchRequest()
        search?.delegate = self
        req.strategy = 0
        
//        search?.aMapTransitRouteSearch(req)
        configMapView()
    }
    
    func configMapView() {
        var lowerPos:CGFloat = distanceLabel.frame.origin.y+distanceLabel.frame.height
        let imgPos:CGFloat = imageView.frame.origin.y+imageView.frame.height
        if(imgPos>lowerPos){
            lowerPos=imgPos
        }
        
        let yOffset = lowerPos+21+(self.navigationController?.navigationBar.frame.height)!
        let mapFrame = CGRect(x: 0, y: yOffset, width: view.frame.width, height: view.frame.height-yOffset)
        mapView = MAMapView(frame: mapFrame)
        mapView?.delegate = self
        mapView?.showsCompass = false
        mapView?.showsScale = false
        search = AMapSearchAPI()
        search?.delegate = self
        locationManager.delegate = self
        locationManager.locatingWithReGeocode = true
        self.view.addSubview(mapView!)
        locationManager.startUpdatingLocation()
        
        destAnnotation = MAPointAnnotation()
        
        destAnnotation?.coordinate.latitude =  CLLocationDegrees((itemInfo?.location.latitude)!)
        destAnnotation?.coordinate.longitude = CLLocationDegrees((itemInfo?.location.longitude)!)
        mapView?.addAnnotation(destAnnotation)
    }

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if((itemInfo?.images.count)!>0){
            do {
                let data = try Data(contentsOf: URL(string: (itemInfo?.images[0].url)!)!)
                imageView.image = UIImage(data: data)
            } catch {
            }
        }
        nameLabel.text = itemInfo?.name
        addressLabel.text = itemInfo?.address
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: - AMapLocationManagerDelegate
    func amapLocationManager(_ manager: AMapLocationManager!, didUpdate location: CLLocation!) {
        var flag = false
        if let a = selfAnnotation {
        }else {
            selfAnnotation = MAPointAnnotation()
            flag = true
        }
        mapView?.removeAnnotation(selfAnnotation)
        selfAnnotation!.coordinate = location.coordinate
        mapView?.addAnnotation(selfAnnotation)
        if flag {
            mapView?.setCenter((selfAnnotation?.coordinate)!, animated: false)
        }
        mapView?.setZoomLevel(14, animated: true)
    }
    
    //MARK: - MAMapViewDelegate
    func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
        var annoView: MAAnnotationView?
        if(annotation as? MAPointAnnotation == selfAnnotation){
            annoView = MAAnnotationView(annotation: annotation, reuseIdentifier: nil)
            annoView?.image = UIImage(named: "my_position")
            annoView?.frame = CGRect(x: 0, y: 0, width: 34, height: 55)
        } else if(annotation as? MAPointAnnotation == destAnnotation){
            annoView = MAAnnotationView(annotation: annotation, reuseIdentifier: nil)
            annoView?.image = UIImage(named: "dest_position")
            annoView?.frame = CGRect(x: 0, y: 0, width: 34, height: 55)
        }
        return annoView
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
